package com.blb.blb_erp.controller;

import com.blb.blb_erp.entity.JsonResult;
import com.blb.blb_erp.entity.SMenu;
import com.blb.blb_erp.entity.SUser;
import com.blb.blb_erp.service.ISMenuService;
import com.blb.blb_erp.vo.TreeVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *菜单控制器
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private ISMenuService menuService;

    @RequestMapping("/findMenus")
    public JsonResult findMenus(){
        try {
            //获得登录用户id,登录用户保存在Session中,可以通过Shiro获取
            SUser user = (SUser) SecurityUtils.getSubject().getPrincipal();
            List<SMenu> menus = menuService.findMenusByUserId(user.getId());
            return new JsonResult(1,menus);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @RequestMapping("/findMenuTrees")
    public JsonResult findMenuTrees(){
        try {
            List<TreeVO> menuTrees = menuService.findMenuTrees();
            return new JsonResult(1,menuTrees);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }

    @RequestMapping("/findRoleMenuIds")
    public JsonResult findCheckedMenuIds(String roleId){
        try {
            List<String> ids = menuService.findMenuIdsByRoleId(roleId);
            return new JsonResult(1,ids);
        }catch (Exception ex){
            ex.printStackTrace();
            return new JsonResult(0,null);
        }
    }
}
